
linkInput1 = "features_train_all.txt"
linkInput2 = "features_train_all_tuvt.txt"
linkOutput = "features_binh_tu.txt"

import sys
sys.path.append('../ReadData/')
import xml2dict

import os
directory_path = os.getcwd()
list_features_select=['1','2','3','4','200','204']

def readData(linkInput):
    list_instances = []
    fin = open(linkInput,'r')
    for line in fin:
        instance = {}
        instance['id'] = line[line.find("#")+1:line.find("\n")]
        line = line[:line.find("#")]       
        line = line.split(None, 1)
        # In case an instance with all zero features
        if len(line) == 1: line += ['']
        instance['label'], features = line
        list_features = features.split(" ")
        for feature in list_features:
            if feature.find(':')!=-1:
                instance[feature[:feature.find(":")]]=feature[feature.find(":")+1:]
        list_instances.append(instance)
    fin.close() 
    return list_instances

def combineFeatures(list_instance1,list_instance2):
    if len(list_instance1)!=len(list_instance2):
        print "Do dai khong bang nhau. Khong the ket hop!"
        return
    list_instances=[]
    for instance1 in list_instance1:
        for instance2 in list_instance2:
            #neu cung id moi ghep
            if instance1['id']==instance2['id']:
                #khong can check label
                for key in instance2.keys():
                    if key!='label' and key!='id':
                        instance1[key]=instance2[key]
        list_instances.append(instance1)
    return list_instances

def writeAllToOutput(list_instances):
    fout = open(linkOutput,'w')
    for instance in list_instances:
        str_out = str(instance['label'])+" "
        for key in instance.keys():
            if key!='id' and key!='label':
                str_out+=key+":"+instance[key]+" "
        print instance['id']
        str_out+="#"+instance['id']+"\n"
        fout.writelines(str_out)
    fout.close()
    
def writeOutput(list_instances,list_features_select):
    fout = open(linkOutput,'w')
    for instance in list_instances:
        str_out = str(instance['label'])+" "
        for key in instance.keys():
            if key in list_features_select:
                str_out+=key+":"+instance[key]+" "
        #print instance['id']
        str_out+="#"+instance['id']+"\n"
        fout.writelines(str_out)
    fout.close()

if __name__ == '__main__':  
    list_instance1 = readData(linkInput1)
    list_instance2 = readData(linkInput2)
    list_instances = combineFeatures(list_instance1,list_instance2)
    writeAllToOutput(list_instances)
    #writeOutput(list_instances,list_features_select)

